/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package EBInterface;

import EntityBeans.Airports;
import EntityBeans.Flightinstances;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author 
 */
@Stateless
public class AirlineSystemBean implements AirlineSystemRemote{

    @PersistenceContext
    EntityManager em;
    
    Collection<Flightinstances> flights;

    public void findFlights(Date d, String dep, String dest, int nrPass) {
        String date = d.toString();
        String query = "SELECT * FROM FlightInstances fi join Flights f where fi.flight = f.nr and " +
                "f.depDate = '" + date + "' and f.depAirport = '" + dep + "' and " +
                "f.destAirport = '" + dest + "' and fi.availableseats >= " + nrPass;
        System.out.println(query);
        Query q = em.createNativeQuery(query, Flightinstances.class);
        flights = q.getResultList();
    }

    public Collection<Flightinstances> getFlights(){
        return flights;
    }

    public Collection<Airports> getAllAirports() {
        Query q = em.createNamedQuery("Airports.findAll");
        Collection<Airports> airports = q.getResultList();
        return airports;
    }

    public void findFlightSeats(List<String> flights) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
